#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#define N 30
#define M N *N
using namespace std;

int map[N][N], mark[N], in[N], tin[N];
char anstr[N];
int n, flag;

void init()
{
    memset(map, 0, sizeof(map));
    memset(mark, 0, sizeof(mark));
    memset(in, 0, sizeof(in));
    memset(tin, 0, sizeof(tin));
}

int topCheck(int id) // 拓扑排序
{
    int i, cnt = 0, pos = -1;
    for (i = 0; i < 26; i++)
    {
        if (mark[i] && !in[i])
        {
            cnt++;
            pos = i;
        }
    }
    if (cnt > 1) // 当有多个入度为0的点
    {
        flag = 1; // 标记出现多个入度为0的点的情况
        in[pos] = -1;
        anstr[id] = pos + 'A';
        for (i = 0; i < 26; i++)
        {
            if (map[pos][i])
                in[i]--;
        }
        return topCheck(id + 1);
    }
    if (cnt == 0)
    {
        for (i = 0; i < 26; i++)
        {
            if (mark[i] && in[i] > 0)
            {
                return 1; // 入度都大于0，出现了环
            }
        }
        if (id != n || flag)
            return 0; // 当排序到入度为0时，并且一直没有出现过多个入度为0的情况，则排序可以完成
        anstr[id] = '\0';
        return 2;
    }
    in[pos] = -1;
    anstr[id] = pos + 'A';
    for (i = 0; i < 26; i++)
    {
        if (map[pos][i])
            in[i]--;
    }
    return topCheck(id + 1); // 进入下一层拓扑排序
    return 0;
}

int main()
{
    int m;
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
            break;
        char xx[5];
        int i;
        init();
        int temp = 0, pos = 0;
        for (i = 1; i <= m; i++)
        {
            scanf("%s", xx);
            int x = xx[0] - 'A';
            int y = xx[2] - 'A';
            mark[x] = mark[y] = 1;
            tin[y]++;
            map[x][y] = 1;
            memcpy(in, tin, sizeof(int) * 26);
            if (temp != 0)
                continue;
            flag = 0;
            temp = topCheck(0);
            pos = i;
        }
        if (temp == 2)
        {
            anstr[pos + i] = '\0';
            printf("Sorted sequence determined after %d relations: %s.\n", pos, anstr);
            continue;
        }
        if (temp == 1)
        {
            printf("Inconsistency found after %d relations.\n", pos);
            continue;
        }
        puts("Sorted sequence cannot be determined.");
    }
    return 0;
}